package com.kx.center.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

import com.kx.center.email.SendMailBase;
import com.kx.center.service.SendEmailService;

/**
 * @description 发送模板邮件服务类
 * @version 1.0
 * @author Scott
 * @date 2016年1月18日 下午1:50:45
 */
@Service
public class SendEmailServiceImpl implements SendEmailService {

	@SuppressWarnings("resource")
	@Override
	public boolean sendEmailFromTemplate(String template, List<String> acceptEmail, Map<String, Object> model) {
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

		// 取出模板
		SendMailBase sendMailImpl = (SendMailBase) context.getBean(template);

		if (sendMailImpl == null) {
			return false;
		}

		for (int i = 0; i < acceptEmail.size(); i++) {
			Map<String, Object> sendModel = new HashMap<>();

			sendModel.put("recipient", acceptEmail.get(i));
			if(model != null){
				sendModel.putAll(model);
			}
			
			//发送邮件
			try {
				sendMailImpl.sendMail(sendModel);
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}
		}
		return true;

	}

}
